import 'package:ai_desktop_eidtor/common/y_text.dart';
import 'package:ai_desktop_eidtor/routes/app_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'book_logic.dart';
import 'book_state.dart';

class BookPage extends StatelessWidget {
  const BookPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final BookLogic logic = Get.put(BookLogic());
    final BookState state = Get.find<BookLogic>().state;

    return Scaffold(
      appBar: AppBar(
        title: YText(text: '书籍列表'),
        backgroundColor: Colors.white,
      ),
      body: Padding(
        padding: const EdgeInsets.all(20.0),
        child: GetBuilder<BookLogic>(
          builder: (_) => GridView.builder(
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: ((ScreenUtil().screenWidth - 50) / 100).toInt(),
              mainAxisSpacing: 10,
              crossAxisSpacing: 10,
              childAspectRatio: 90 / 120,
            ),
            itemBuilder: (ctx, index) {
              return GestureDetector(
                onTap: () {
                  Get.toNamed(RouteName.bookDetail, arguments: logic.dataArr[index].id)?.then((v) {
                    if (v != null) {
                      logic.loadAllBook();
                    }
                  });
                },
                child: Container(
                  padding: EdgeInsets.only(left: 6, right: 6),
                  decoration: BoxDecoration(color: Colors.red, borderRadius: BorderRadius.circular(10)),
                  child: Center(
                    child: YText(text: logic.dataArr[index].title, color: Colors.white, maxLines: 10),
                  ),
                ),
              );
            },
            itemCount: logic.dataArr.length,
          ),
        ),
      ),
    );
  }
}
